package com.dlb.andorid.activity;

import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.dlb.andorid.R;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;

import java.util.HashMap;
import java.util.Map;

public class WebPayActivity extends BaseCommonActivity {


	private boolean isFirstLoad;

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_web_pay;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected BasePresenter getBasePresenter() {
		return null;
	}

	protected String mUrl;

	@Override
	protected void beForeSetContentView() {
		super.beForeSetContentView();
		mUrl = (String) getIntentData("url");
	}

	@Override
	protected void initData() {
		loadWeb();
	}

	@Override
	protected String getTitleTxt() {
		return "订单支付";
	}

	@Override
	public String getPageName() {
		return null;
	}

	protected WebView tbsWebView;

	protected void loadWeb() {

		tbsWebView = findViewById(R.id.webView);
		WebSettings settings = tbsWebView.getSettings();
		settings.setJavaScriptEnabled(true);
		//https与http混合资源处理
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			settings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
		}
		// 设置加载进来的页面自适应手机屏幕
		settings.setUseWideViewPort(true);
		settings.setLoadWithOverviewMode(true);
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		settings.setDisplayZoomControls(true);
		settings.setDefaultTextEncodingName("UTF-8");
		settings.setJavaScriptCanOpenWindowsAutomatically(true);

		//settings.setUserAgent( );
		final String Referer = "http://www.dilinbang.com";
		tbsWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		tbsWebView.setWebViewClient(new WebViewClient() {

			@Override
			public void onLoadResource(WebView view, String url) {
				super.onLoadResource(view, url);
			}

			@Override
			public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
				super.onReceivedHttpError(view, request, errorResponse);
			}

			@Override
			public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
				handler.proceed();  // 接受所有网站的证书
				super.onReceivedSslError(view, handler, error);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.e("kwan","shouldOverrideUrlLoading:"+url);
				if (url.startsWith("weixin://wap/pay?")) {
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setData(Uri.parse(url));
					startActivity(intent);
					return true;
				} else {
					Map<String, String> extraHeaders = new HashMap<>();
					extraHeaders.put("Referer", Referer);
					view.loadUrl(url, extraHeaders);
					return true;
				}
			}
		});
		//需要添加到支付链接的请求头
		//String targetUrl=";  //后台返回的支付链接

		Map<String, String> extraHeaders = new HashMap<>();
		extraHeaders.put("Referer", Referer);
		tbsWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				super.onProgressChanged(view, newProgress);
			}

		});

		tbsWebView.loadUrl(mUrl, extraHeaders);

	}
}
